<?php
namespace Common\Controller;
use Think\Controller;
class BaseIpadmtController extends Controller {
    protected $uinfo = '';    //用户基本信息
    protected $req_from = ''; //请求入口

    //初始化
	function _initialize() {
        $ip = get_client_ip(0, 1); //登录者ip
        if($ip=='::1' || $ip=='0.0.0.0' || $ip=='127.0.0.1' || $ip=='0.0.0.0'){
            $ip = '192.168.0.1';
        }
        $this->ip = $ip;
        $this->webLogin();
        $this->check_ismobile();
	}

    //WEB应用登陆检测
    protected function webLogin(){
        $uinfo = is_login_cc(I('token'));
        if($uinfo){
            define('UID', $uinfo['uid']);
            define('USER_NAME', $uinfo['username']);
            $this->uinfo = $uinfo;
        }else{
            define('UID', null);
        }
    }
      //检测程序是ipad 还是手机访问
      protected function check_ismobile(){
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $is_pc = (strpos($agent, 'windows nt')) ? true : false;
        $is_ipad = (strpos($agent, 'ipad')) ? true : false;
        $is_iphone = (strpos($agent, 'iphone')) ? true : false;
        if($is_iphone){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请用IPAD或者PC访问";
            exit(json_encode($data_cc));
        }
    }

	//ajax 成功信息返回
	function ajaxSucReturn($data,$code=200){
        $msg = [];
		if(is_array($data)){
            if(isset($data['data'])){
                $msg = array_merge($msg,$data);
            }else{
                $msg['data'] = $data;
            }
		}else{
			$msg['msg'] = $data;
		}
        if(!$msg['msg']){
            $msg['msg'] = '查询成功';
        }
        $msg['code'] = $code;
        unset($msg['sqls']);
		$this->ajaxReturn($msg,'JSON');
	}

	function ajaxErrReturn($data, $error_code=0){
        $msg = [];
		if(is_array($data)){
			$msg = array_merge($msg,$data);
			if(!$msg['errcode'])$msg['errcode'] = (string)$error_code;
		}else{
			$msg['msg'] = $data;
			$msg['code'] = (string)$error_code;
		}
        unset($msg['sqls']);
		$this->ajaxReturn($msg,'JSON');
	}


}
